<?php
/*
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip != '125.123.66.158') {
  //header('Location: http://dreamlife.co.nz/pz/tmp/dms2');
  header( 'Location: ../maintenance.html');
  return;
}
*/

if (strpos(curPageURL(), 'www.') !== false) {
  header( 'Location: ' . str_replace('www.', '', curPageURL()));
  return;
}


function curPageURL() {
  $pageURL = 'http';
  $pageURL .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  }
  return $pageURL;
}

require_once dirname(__FILE__) . '/includes/common.inc.php';

if (isset($_COOKIE[OPERATOR])) {
  $_SESSION[OPERATOR] = json_decode($_COOKIE[OPERATOR]);
}

$path = MiscUtils::getParam('p', 'pagelogin') . '/';
$template = MiscUtils::getParam('t', 'pageuserlogin');
$menu = MiscUtils::getParam('m', json_encode(array()));
$options = MiscUtils::getParam('opts', json_encode(array()));

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[OPERATOR]);
  setcookie(OPERATOR, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('path', 'pagelogin/');
  $smarty->assign('template', 'pageuserlogin');
  $smarty->display("pagelogin/base.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[OPERATOR])) {
  if ($_SESSION[OPERATOR]->type == USER_TYPE_FACTORY) {
    $path = 'pagefactory/';
    $template = ($template == 'pageuserlogin') ? 'pageb1factoryworks' : $template;
    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract') {
      $menu = new stdClass();
      $menu->a = 1;
      $menu->b = 1;
      $menu->c = 1;
      $menu = json_encode($menu);
    }
  } else if ($_SESSION[OPERATOR]->type == USER_TYPE_CUSTOMER_SERVICE) {
    $path = 'pagedesigner/';
    $template = ($template == 'pageuserlogin') ? 'pageb1designerworks' : $template;
    if ($menu == json_encode(array()) && $template != 'pagereceipt' && $template != 'pagecontract') {
      $menu = new stdClass();
      $menu->a = 1;
      $menu->b = 1;
      $menu->c = 1;
      $menu = json_encode($menu);
    }
    $menu = json_encode($menu);
  }
  
  $smarty->assign('userInfo', $_SESSION[OPERATOR]->account . ' (' . (((mb_substr($_SESSION[OPERATOR]->type, 0, 1) == '#') ? mb_substr($_SESSION[OPERATOR]->type, 1) : $_SESSION[OPERATOR]->type) . ')'));
  
  $smarty->assign('path', $path);
  $smarty->assign('template', $template);

  $smarty->assign('menu', json_decode($menu));
  
  $smarty->assign('operator', json_encode($_SESSION[OPERATOR]));
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;
  $smarty->assign('now', json_encode($now));
  $smarty->assign('options', urldecode($options));
  $smarty->display($path . 'base.tpl');
  return;
} else {
  $smarty->assign('path', 'pagelogin/');
  $smarty->assign('template', 'pageuserlogin');
  $smarty->display("pagelogin/base.tpl");
  return; /* SignIn Required */
}
?>